package com.hillky.emos.wx.config.aop;


import cn.hutool.core.util.StrUtil;
import com.hillky.emos.wx.config.shiro.ThreadLocalToken;
import com.hillky.emos.wx.utils.R;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class TokenAspect {

    @Autowired
    private ThreadLocalToken threadLocalToken;

    @Pointcut("execution(public * com.hillky.emos.wx.controller.*.*(..))")
    public void aspect(){

    }


    @Around("aspect()")  //环绕执行。请求和响应都要经过这里
    public Object around(ProceedingJoinPoint point) throws Throwable {

        R r = (R) point.proceed();
        String token = threadLocalToken.getToken();
        if(StrUtil.isNotBlank(token)){
            //有新token
            r.put("token",token);
            threadLocalToken.clear();
        }
        return r;
    }
}
